package com.kingyun.gpsinspection.purificationservice.services.iam.outerservice.adm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.BusinessTravelMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.BusinessTravel;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.TaskLinkIam;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.BPMService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.OutBusinessTravelService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TaskLinkIamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;

/**
 * Created by yangsy on 2016/12/19.
 */
@Service
public class OutBusinessTravelServiceImpl implements OutBusinessTravelService
{
    @Autowired
    private BusinessTravelMapper businessTravelMapper;
    @Autowired
    private TaskLinkIamService taskLinkIamService;
    @Autowired
    private BPMService bpmService;
    @Override
    public int deleteByPrimaryKey(String businessTravelId) {
        return businessTravelMapper.deleteByPrimaryKey(businessTravelId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(BusinessTravel record,String userName,String pwd) {
        int count=0;
        //设置状态 提交
        record.setStatus(BusinessTravel._STATUS_SUBMIT);
        //开启流程
        String ret=bpmService.openBPM(1,userName,pwd,null);
        //保存任务环节
        TaskLinkIam taskLink=  new TaskLinkIam();
        taskLinkIamService.save(taskLink,ret,(short)1,record.getBusinessTravelId(),(short)1);
        //完成流程
        bpmService.completeTask(taskLink.getTkiid(), userName, pwd, null);
        count += businessTravelMapper.insertSelective(record);
        return  count;
    }

    @Override
    public BusinessTravel selectByPrimaryKey(String businessTravelId) {
        return businessTravelMapper.selectByPrimaryKey(businessTravelId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(BusinessTravel record,TaskLinkIam taskLinkIam,String userName,String pwd) {
        int count=0;
        //用户信息
        String username = userName;
        String password = pwd;
        short note=1;
        String parameter=null;
        try{
            //判断节点位置
            if (taskLinkIam.getRole().equals("部门经理")) {
                note = 2;
                String statue = record.getStatus().toString();
                //流程走向参数
                parameter = "{'isagree':'" + statue + "'}";
                //转换成utf-8
                parameter = URLEncoder.encode(parameter, "utf-8");
                //设置任务环节审批状态
                taskLinkIam.setAuditStatus(record.getStatus());
                //设置任务环节备注
                taskLinkIam.setRemark(record.getComments());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //处理流程
        String result = bpmService.completeTask(taskLinkIam.getTkiid(), username, password, parameter);
        //保存任务环节
        taskLinkIamService.save(taskLinkIam, result, (short)2, record.getBusinessTravelId(), note);
        count += businessTravelMapper.updateByPrimaryKeySelective(record);
        return  count;
    }

    @Override
    public JSONObject list(BusinessTravel businessTravel) {
        JSONObject jsonObject = new JSONObject();
        businessTravel.setLimit( businessTravel.getLimit() == null ? Integer.valueOf(10) : businessTravel.getLimit() );
        businessTravel.setOffset( businessTravel.getOffset() == null ? Integer.valueOf(0) : businessTravel.getOffset() );
        jsonObject.put("total",businessTravelMapper.queryCount(businessTravel));
        jsonObject.put("rows", JSON.toJSON(businessTravelMapper.queryList(businessTravel)));
        return jsonObject;
    }

    /**
     * 查询代处理
     * @param bootstrapSearchBean
     * @param task
     * @param type 0 休假1出差2用车
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject listToDoTask( BootstrapSearchBean bootstrapSearchBean, TaskLinkIam task, int type,String username,String passwor){
        JSONObject jsonObject = new JSONObject();
        try{
            jsonObject = taskLinkIamService.listToDoTask(bootstrapSearchBean,task,type,username,passwor);
        }catch(Exception e){
            e.printStackTrace();
        }
        return  jsonObject;
    }
}
